package org.jivesoftware.smackx.workgroup.util;

import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.ListIterator;

public class ListenerEventDispatcher
  implements Runnable
{
  protected transient boolean hasFinishedDispatching;
  protected transient boolean isRunning;
  protected transient ArrayList triplets = new ArrayList();

  public ListenerEventDispatcher()
  {
    this.hasFinishedDispatching = false;
    this.isRunning = false;
  }

  public void addListenerTriplet(Object paramObject, Method paramMethod, Object[] paramArrayOfObject)
  {
    if (!(this.isRunning))
      this.triplets.add(new TripletContainer(this, paramObject, paramMethod, paramArrayOfObject));
  }

  public boolean hasFinished()
  {
    return this.hasFinishedDispatching;
  }

  public void run()
  {
    this.isRunning = true;
    ListIterator localListIterator = this.triplets.listIterator();
    if (!(localListIterator.hasNext()))
    {
      this.hasFinishedDispatching = true;
      return;
    }
    TripletContainer localTripletContainer = (TripletContainer)localListIterator.next();
    try
    {
      localTripletContainer.getListenerMethod().invoke(localTripletContainer.getListenerInstance(), localTripletContainer.getMethodArguments());
    }
    catch (Exception localException)
    {
      System.err.println("Exception dispatching an event: " + localException);
      localException.printStackTrace();
    }
  }

  protected class TripletContainer
  {
    protected Object listenerInstance;
    protected Method listenerMethod;
    protected Object[] methodArguments;

    protected TripletContainer(, Object paramObject, Method paramMethod, Object[] paramArrayOfObject)
    {
      this.listenerInstance = paramObject;
      this.listenerMethod = paramMethod;
      this.methodArguments = paramArrayOfObject;
    }

    protected Object getListenerInstance()
    {
      return this.listenerInstance;
    }

    protected Method getListenerMethod()
    {
      return this.listenerMethod;
    }

    protected Object[] getMethodArguments()
    {
      return this.methodArguments;
    }
  }
}